package com.qingluan.widget;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import com.qingluan.Database.Personal_Info_database;
import com.qingluan.Database.TempDataBase;
import com.qingluan.arguments.ARGUMENTS;
import com.qingluan.dd.InetTool.BitmapDownloader;
import com.qingluan.dd.InetTool.BitmapDownloader.CallBack;
import com.qingluan.dd.util.FileCache;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.widget.ImageView;

public class BitmapHelper {
	private Context context;
	private static String Do_path ;
	
	public BitmapHelper(Context context){
		this.context = context;
		Do_path =  ARGUMENTS.getPicturePath();
	}
	
	
	public void  MyTagSave(String user,String tag,String uri){
		Personal_Info_database DB = new Personal_Info_database(context);
		ContentValues values = new ContentValues();
		values.put("mission_name", user );
		values.put("picture_url", uri);
		values.put("tag", tag);
		DB.insertMyTag(values);
		
	}
	
	public void  BitmapLoader(final String picture_info,String time_s,final String email,final String obj_id,final  onBitmapLoaded onloaded) throws IOException{

		String filePath = new TempDataBase(context).searchById(obj_id, TempDataBase.PICTURE);
		
		if (filePath != null){
			Log.d( "I_GET","picture in local path :"+filePath);
			onloaded.onLoadByLocal(filePath);
			
		}else {
			//do web Download-er
			BitmapDownloader.Download(this.context,picture_info,new CallBack(){

				@Override
				public void onDataGot(byte[] data) {
					onloaded.onLoadByWeb(data);
				}
				
			});
		}

		
	}
	public interface onBitmapLoaded{
		public void onLoadByWeb(byte[] data);
		public void onLoadByLocal(String uri);
	}
	
	public static String TurnToBase64(byte[] bytes){
		String base64_string = null;
		base64_string = Base64.encodeToString(bytes, Base64.DEFAULT);
		return base64_string;
	}
	
	public static  byte[] getByteByBitmap(Bitmap bitmap){
		byte[] data = null;
		ByteArrayOutputStream BOS = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG,100,BOS);
		data = BOS.toByteArray();
		try{
			BOS.close();
		}catch (IOException e){
			e.printStackTrace();
		}
		return data;
	}
	
	
	
	public static Uri takePhoto(String imageName,Activity activity){
		String PicturePath = ARGUMENTS.getPicturePath();
		File image = new File(PicturePath + "/"+imageName);
		Uri image_uri = Uri.fromFile(image);
		Intent intent  = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
		activity.startActivityForResult(intent, ARGUMENTS.TakephotoCode);
		return image_uri;
	}
	
	public void CropImageByUri(Uri imageUri,int outputX, int outputY,int requestCode,Activity activity){
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(imageUri, "image/*");
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 2);
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", outputX);
		intent.putExtra("outputY", outputY);
	    intent.putExtra("scale", true);
	    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
	    intent.putExtra("return-data", false);
	    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
	    intent.putExtra("noFaceDetection", true); // no face detection
	    activity.startActivityForResult(intent, requestCode);
		
	}
	
	public static String SaveImage(String name,byte[] data ){
		String picture_name = name+".png";
		String path = ARGUMENTS.getPicturePath()+"/"+picture_name;
		
//		String [] files = new File(ARGUMENTS.getPicturePath()).list();
		File localImage = new File(path);
		try {
			FileCache.saveFile(localImage, data);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return path;
		
	}


//	public static  void BitmapLoaderFromPersonDB(String uid) {
//		// TODO Auto-generated method stub
//		
//	}
	public static Bitmap GetBitmapByUri (String uri) {
		Bitmap bitmap = BitmapFactory.decodeFile(uri);
		return bitmap;
		
	}


	public void BitmapLoader(String uid, ImageView v) {
		// TODO Auto-generated method stub
		
	}
}
